/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package se.fk.klistret.console.ant.tasks;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import java.io.File;
import java.io.IOException;

/**
 * 2011-07-11, The comments below are from 2005 and the code is has since
 * upgraded to the Ant 1.8 platform. The patterns and design of this project
 * should not be reused since they are not considered of any quality. The code
 * was written quickly without long term concerns.
 * <p>
 * 
 * 2005, Gets the file length
 * 
 * @author Matthew Young
 * 
 */
public class FileSizeTask extends Task {
	/**
	 * File object representing the file in question concerning size.
	 */
	private File file;
	/**
	 * Name of the property to hold the file size.
	 */
	private String property;

	/**
	 * (Required by Ant Task implementation) Executes a simple <b><font
	 * size="-1">length</font></b> method part of the <code>
	 * File</code> class and sets the property.
	 * <p>
	 * 
	 * @throws BuildException
	 *             Thrown if the receiving property name is not set or if the
	 *             file parameter is missing.
	 * 
	 */
	public void execute() throws BuildException {

		validateAttributes();

		getProject().setProperty(property, Long.toString(file.length()));

	}

	/**
	 * <ul>
	 * Validates attributes set by reflextion:
	 * <li>file</li>
	 * <li>property</li>
	 * </ul>
	 * <p>
	 * 
	 * @throws BuildException
	 *             Property or file attribute not set
	 * 
	 */
	private void validateAttributes() {

		if (property == null) {
			throw new BuildException("Property not set as attribute",
					getLocation());
		}

		if (file == null) {
			throw new BuildException("File not set as attribute", getLocation());
		}

	}

	/**
	 * Sets file in question of size. <b>file</b> parameter is reflexed into
	 * this method.
	 * <p>
	 * 
	 * @param href
	 *            <code>String</code>
	 * 
	 * @throws BuildException
	 *             Thrown if the file does not exist, is not recognized as a
	 *             file, or as an unchecked IOException.
	 * 
	 */
	public void setFile(String href) throws BuildException {
		try {
			file = new File(href);

			if (!file.getCanonicalPath().equals(file.getPath())) {
				file = new File(file.getCanonicalPath());

				href = file.getPath();
			}

			if (!file.exists()) {
				throw new BuildException("File (" + href
						+ ") does not exist (verify source if link)",
						getLocation());
			}

			if (!file.isFile()) {
				throw new BuildException("File (" + href
						+ ") is not recognized as a file", getLocation());
			}
		} catch (IOException ie) {
			throw new BuildException(ie.getMessage(), getLocation());
		}
	}

	/**
	 * Sets property receiving file size. <b>property</b> parameter is reflexed
	 * into this method.
	 * <p>
	 * 
	 * @param property
	 *            <code>String</code> representing the property name.
	 * 
	 */
	public void setProperty(String property) {
		this.property = property;
	}
}
